//
//  BIDFunctionView.m
//  HJGJCommunity
//
//  Created by mal on 2017/5/18.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDFunctionView.h"
#import "BIDFunctionSubCell.h"

@interface BIDFunctionView()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    NSArray *_dataSourceArr;
}
@end

@implementation BIDFunctionView
@synthesize delegate;

- (void)awakeFromNib
{
    [super awakeFromNib];
    [_collectionView registerClass:[BIDFunctionSubCell class] forCellWithReuseIdentifier:@"identifier"];
    [self prepareForData];
}

- (void)prepareForData
{
    _dataSourceArr = @[@{@"title":@"通知", @"imgName":@"function_notice.png", @"className":@"BIDNoticeListViewController", @"isNeedXib":@0, @"isNeedBindingHouse":@0},
                       @{@"title":@"缴费", @"imgName":@"function_pay.png", @"className":@"BIDPayListViewController", @"isNeedXib":@0, @"isNeedBindingHouse":@1},
                       @{@"title":@"报修", @"imgName":@"function_repair.png", @"className":@"BIDRepairKindViewController", @"isNeedXib":@1, @"isNeedBindingHouse":@1},
                       @{@"title":@"建议", @"imgName":@"function_advice.png", @"className":@"BIDSuggestionAndPraiseViewController", @"isNeedXib":@1, @"isNeedBindingHouse":@1},
                       @{@"title":@"社区活动", @"imgName":@"function_activity.png", @"className":@"BIDActivityViewController", @"isNeedXib":@0, @"isNeedBindingHouse":@0},
                       @{@"title":@"物业工作", @"imgName":@"function_wuye.png", @"className":@"BIDPropertyWorkViewController", @"isNeedXib":@1, @"isNeedBindingHouse":@0},
                       @{@"title":@"快递查询", @"imgName":@"function_expressQuery.png", @"className":@"BIDExpressQueryViewController", @"isNeedXib":@1, @"isNeedBindingHouse":@0},
                       @{@"title":@"更多", @"imgName":@"function_more.png", @"className":@"BIDMoreModuleViewController", @"isNeedXib":@1, @"isNeedBindingHouse":@0}];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [flowLayout setItemSize:CGSizeMake(CGRectGetWidth(self.frame)/4, 65.f)];
    [flowLayout setMinimumLineSpacing:10];
    [flowLayout setMinimumInteritemSpacing:0];
    [flowLayout setSectionInset:UIEdgeInsetsMake(13.f, 0.f, 0.f, 0.f)];
    [_collectionView setCollectionViewLayout:flowLayout];
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    BIDFunctionSubCell *functionCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    NSDictionary *dic = _dataSourceArr[row];
    functionCell.imgView.image = [UIImage imageNamed:dic[@"imgName"]];
    functionCell.titleLabel.text = dic[@"title"];
    if((row+1)%4 == 0)
    {
        functionCell.separatorLabel.hidden = YES;
    }
    else
    {
        functionCell.separatorLabel.hidden = NO;
    }
    cell = functionCell;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.row);
    NSUInteger row = indexPath.row;
    NSDictionary *dic = _dataSourceArr[row];
    NSNumber *value = dic[@"isNeedXib"];
    BOOL isNeedXib = [value intValue]==1 ? YES : NO;
    value = dic[@"isNeedBindingHouse"];
    BOOL isNeedBindingHouse = [value intValue]==1 ? YES : NO;
    [delegate openModuleWithClassName:dic[@"className"] andXib:isNeedXib bindingHouse:isNeedBindingHouse];
}

@end
